Page({
  data: {
    startTime: wx.$util.formatYMD(new Date()),
    endTime: wx.$util.formatYMD(new Date()),
    startShow: false,
    endShow: false,
    timeIndex: 1,
    enterNum: '0',
    outNum: '0',
    stayNum: '0',
    minDate: new Date(2022, 0, 1).getTime(),
    detailList: []
  },

  bindDateChange (e) {
    let that = this;
    let timeIndex = e.currentTarget.dataset.index;
    if (timeIndex == 1) {
      that.setData({
        startTime: e.detail.value,
        timeIndex
      });
    } else if (timeIndex == 2) {
      that.setData({
        endTime: e.detail.value,
        timeIndex
      });
    }
    this.getWeiXiuData()
  },
  onShow () {
    this.getWeiXiuData();
  },
  // onClose () {
  //   if (this.data.timeIndex == 1) {
  //     this.setData({ startShow: false });
  //   } else if (this.data.timeIndex == 2) {
  //     this.setData({ endShow: false });
  //   }
  // },
  formatDate (date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },

  getWeiXiuData () {
    let that = this;
    const begin_date = that.data.startTime
    const end_date = that.data.endTime
    wx.showLoading({});
    wx.$get(5, `BC/WeiXiuTongJi/${begin_date}/${end_date}`).then((res) => {
      wx.hideLoading({});
      if (res.response.errcode != 0) return wx.$toast(res.response.errmsg);
      const data = res.data.row[0]
      that.setData({
        enterNum: data.jinchangshuliang,
        outNum: data.chuchangshuliang,
        stayNum: data.daixiushuliang,
        detailList: data.details
      })
    })
  },
  // onConfirm (event) {
  //   console.log(event)
  //   const that = this
  //   if (that.data.timeIndex == 1) {
  //     that.setData({
  //       startShow: false,
  //       startTime: that.formatDate(event.detail),
  //     });
  //   } else if (that.data.timeIndex == 2) {
  //     that.setData({
  //       endShow: false,
  //       endTime: that.formatDate(event.detail),
  //     });
  //   }
  //   this.getWeiXiuData()
  // },
  handleDetail () {
    const that = this
    wx.navigateTo({
      url: '/repairPages/service_detail/service_detail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('detailList', that.data.detailList)
      }
    })
  }
})
